<template>
  <DataList 
  
  :excelHeader="excelHeader"
  :cate="cate" 
  :cols="cols"  
  :name="name"
  ref="datalist"
  skipExcelNumber="4"

  ><template slot="search">
     <el-form-item >
        <el-input v-model="search_str"  @change="clear" @input="mysearch" placeholder="请输入姓名或学号搜索" clearable></el-input>
      </el-form-item>
  </template></DataList>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get在校生信息By班级,get在校生信息ByXSXH} from "@/api/在校生信息"
import {get班级,get在校生信息} from "@/api/在校生信息"
import linq from "linq"
import { addXsjyxx, deleteXsjyxx, updateXsjyxx, getXsjyxx,bulkAddXsjyxx } from "@/api/学生就业信息.js";
import {findComponent} from "@/utils/ui";
import { Promise } from 'q';
export default {
  components: {
    DataList
  },
  mounted() {
    console.log(this.$root)
  },
  data() {
    
    return {
      search_str:'',
      cate: "A10-1-2学生就业信息",
      name:'10.1.2 学生就业信息表',
      学生:[],
      excelHeader:{B3:"基本信息",G3:"就业情况",B4:"班级代码",C4:"班级名称（全称）",D4:"学号",E4:"姓名",F3:"毕业去向",G4:"就业单位名称（全称）",H4:"就业单位情况",J4:"起薪线",K4:"是否对口",L4:"就业区域",H5:"性质",I5:"规模",L5:"省市",M5:"地市"},
      cols: [
        { label: "班级代码", prop: "BJDM" },
        { label: "班级名称", prop: "BJMC" },
        { label: "学号", prop: "XSXH" },
        { label: "姓名", prop: "XM" },
        { label:'毕业去向',prop:'XZ_BYQX'},  
        { label: "就业情况", prop: "就业情况" ,
            data:[  
            {label:'就业单位名称',prop:'JYDWMC'},
            {label:'就业单位情况',prop:'就业单位情况',
              data:[
                  {label:'性质',prop:'XZ_JYDWXZ'},
                  {label:'规模',prop:'XZ_JYDWGM'},
              ]
            },
            {label:'起薪线',prop:'QXX'},
            {label:'是否对口',prop:'XZ_SFDK'},
            {label:'就业区域',prop:'就业区域',
              data:[
                  {label:'省市',prop:'XZ_SZDQ_SS'},
                  {label:'地市',prop:'XZ_SZDQ_DS'},
              ]
            },                
        ]},               
      ]
    };
  },
   methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]= "合计";
        sums[6]= "班级数:"+linq.from(list).distinct("$.BJMC").count();
        sums[8]= "学生数:"+linq.from(list).distinct("$.XSXH").count();
        sums[10]= "就业:"+linq.from(list).where("$.XZ_BYQX!=''").count();
        sums[11]= "单位数:"+linq.from(list).where("$.JYDWMC!=''").count();
        sums[14]= "平均:"+(linq.from(list).sum("$.QXX")/linq.from(list).where("$.XZ_BYQX!=''").count()).toFixed(2);
        sums[15]= "对口:"+linq.from(list).where("$.XZ_SFDK=='是'").count();
        sums[18]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[18] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[18] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        
       return sums;
      //
    },
      //导入检查方法
     check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var 在校生 = await get在校生信息(user.XXID,user.CJPC)
          //省检查
          var    cities = require("../../../../../static/city.json");
           var err = linq.from(array).where("$.XZ_SZDQ_SS").select("$.XZ_SZDQ_SS").except(linq.from(cities.provinces).select("$.name").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XZ_SZDQ_SS) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"不存在相应的省份");
          //市检查
          linq.from(array).where("$.XZ_SZDQ_SS  && $.XZ_SZDQ_DS ").where(b=>{
          return linq.from(cities.provinces).where(c=>c.name ==b.XZ_SZDQ_SS).any(d=>{
           return d.cities.map(e=>e.name).indexOf(b.XZ_SZDQ_DS) ==-1;
          })}).forEach(e=>e.error=e.error?e.error+";":''+"对应的省份不存在相应的地区");
          //本身重复性检查
          
          var duplicates =  linq.from(array).groupBy("$.XSXH").where("$.count()>1").toArray();
          duplicates.forEach(e=>{
            linq.from(array).where(t=>t.XSXH==e.key()).forEach(t=>t.error="录入数据存在相同的学号");
          })
          //是否已经存在数据
           
          var err =  linq.from(array).select("$.XSXH").intersect(linq.from(list).select("$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在对应的学号");
        
          //是否已经存在相同
          
          err =  linq.from(array).select("$.XSXH").except(linq.from(在校生).select("$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"学生信息表不存在对应的学号");
          //把错误的班级代码和班级名称，学生姓名替代掉
          
          err =  linq.from(array).select("$.BJDM+','+$.XSXH").except(linq.from(在校生).select("$.BJDM+','+$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.BJDM+','+e.XSXH) !==-1).
         forEach(e=>{
            var d = linq.from(在校生).firstOrDefault(item=>item.XSXH == e.XSXH);
            if(d ){
              e.BJDM = d.BJDM;
              e.BJMC = d.BJMC;
              e.XM = d.XM;
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
     
     onDlgCreated(model) {
      var schema = require("../../../../../static/city.json");
      var ss = findComponent(this,"XZ_SZDQ_SS");
      var tt = schema.provinces.map(item => {
        return { label: item.name, value: item.name };
      });
      ss.$attrs.items = tt;
      //
      var ss = findComponent(this,"XZ_SZDQ_SS");
      //
    },


   async  onChange(model, field, value) {
      //

      if (field == "XZ_SZDQ_SS") {
        var schema = require("../../../../../static/city.json");
        var ss = findComponent(this,"XZ_SZDQ_DS");
        var item = schema.provinces.find(item => {
          return item.name == value;
        });
        ss.$attrs.items = item.cities.map(item => {
          return { value: item.name, label: item.name };
        });
        model.XZ_SZDQ_DS = ss.$attrs.items[0].value; 
      }
      if(field == "XSXH"){
         if(value == "error"){
           console.log("error")
          model.XSXH=""
         
          model.XM= "";
          model.BJDM = "";
          model.BJMC ="";
         }else{
         var d1 =  await get在校生信息ByXSXH(this.$root.user.XXID,this.$root.user.CJPC,value);
        
          console.log("udpate ")
          model.XSXH=d1[0].XSXH;
          model.XM= d1[0].XM;
        
          model.BJDM = d1[0].BJDM;
          model.BJMC = d1[0].BJMC;
         
         }
       }
      // if(field == "BJMC"){
      //   //
        
      //   var d1 = linq.from(this.$root.班级).first(item=>item.BJMC==value);
      //   model.BJDM = d1.BJDM;
      //   //
      //   var ss = findComponent(this,"XM");
      //   var students = await get在校生信息By班级(model.BJDM);
        
      //   ss.$attrs.items =  linq.from(students).select("{...$,value:$.XM}").toArray();
        
      //    model.XM = ss.$attrs.items[0].XM; 
      //    model.XSXH =ss.$attrs.items[0].XSXH;
      //   //
      //   this.学生=students;
      // }
      //  if(field == "XM"){
         
      //    var d1 = linq.from( this.学生).first(item=>item.XM==value);
      //    model.XSXH=d1.XSXH;
      //  }
      
    },
    
    async fetchData() {
      var user = getUser();
      var datas = await getXsjyxx(user.XXID,user.CJPC);
      return datas;
    },
    add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.XSXH ==  model.XSXH);
          
          if(first)
          return "已经存在对应学号的学生"
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
      try{     
          
          console.log(JSON.stringify(temp));
          var model = await addXsjyxx(temp);
          console.log(model)
          resolve(model);
        }catch(e){
        reject(e);
      }  
      });
      
    },
    async  bulkAdd(array){
       await  bulkAddXsjyxx({Xsjyxx:array});
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateXsjyxx(temp);
          console.log(model)
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
         await deleteXsjyxx(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>